<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Metadata_model extends WS_model
{

    public function updateMissingKeys($grouping_id, $language_id)
    {
        $this->db->select("md.metadata_seq_id,$language_id as language_seq_id, md.default_value");
        $this->db->from('metadata md');
        $this->db->join('metadata_values mdv', "md.metadata_seq_id = mdv.metadata_seq_id AND mdv.language_seq_id = $language_id", 'left');
        $this->db->where('mdv.metadata_seq_id is null');
        $this->db->where('md.lg_seq_id', $grouping_id);
        $select_sql = $this->sql();
        $sql = 'INSERT INTO {PRE}metadata_values
                (metadata_seq_id, language_seq_id, metadata_value)
                (' . $select_sql . ')';
        $this->db->query($sql);
    }

    public function updateMissingTextKeys($grouping_id, $language_id)
    {
        $this->db->select("lt.lt_seq_id,$language_id as language_seq_id, lt.lt_default_value");
        $this->db->from('language_text lt');
        $this->db->join('language_text_value ltv', "lt.lt_seq_id = ltv.lt_seq_id AND ltv.language_seq_id = $language_id", 'left');
        $this->db->where('ltv.lt_seq_id is null');
        $this->db->where('lt.lg_seq_id', $grouping_id);
        $select_sql = $this->sql();
        $sql = 'INSERT INTO {PRE}language_text_value
                (lt_seq_id, language_seq_id, ltv_value)
                (' . $select_sql . ')';
        $this->db->query($sql);
    }


    public function get_metadata()
    {
        $this->db->select('m.meta_data_group,m.meta_data_key,mv.metadata_value,m.lg_seq_id');
        $this->db->from('metadata_values mv');
        $this->db->join('metadata m', 'm.metadata_seq_id = mv.metadata_seq_id', 'left');
        $this->db->join('language_grouping lg', 'lg.lg_seq_id = m.lg_seq_id AND mv.language_seq_id = lg.language_seq_id', 'left');
        $this->db->where('lg.language_seq_id IS NOT NULL');
        $this->db->order_by('m.meta_data_group', 'asc');
        return $this->result();
    }

    public function get_languagedata()
    {
        $this->db->select('lt.lt_data_group,lt.lt_data_key,ltv.ltv_value,lt.lg_seq_id');
        $this->db->from('language_text_value ltv');
        $this->db->join('language_text lt', 'lt.lt_seq_id = ltv.lt_seq_id', 'left');
        $this->db->join('language_grouping lg', 'lg.lg_seq_id = lt.lg_seq_id AND ltv.language_seq_id = lg.language_seq_id', 'left');
        $this->db->where('lg.language_seq_id IS NOT NULL');
        $this->db->order_by('lt.lt_data_group', 'asc');
        return $this->result();
    }


    public function groupsOfGroupingLanguage($groping_id, $language_id)
    {
        $this->db->from('metadata m');
        $this->db->join('metadata_values mv', 'mv.metadata_seq_id = m.metadata_seq_id', 'left');
        $this->db->where('m.lg_seq_id', $groping_id);
        $this->db->where('mv.language_seq_id', $language_id);
        $this->db->group_by('m.meta_data_group');
        $this->db->order_by('m.meta_data_group', 'asc');
        return $this->result();
    }

    public function groupsOfGroupingLanguage_text($groping_id, $language_id)
    {
        $this->db->from('language_text lt');
        $this->db->join('language_text_value ltv', 'ltv.lt_seq_id = lt.lt_seq_id', 'left');
        $this->db->where('lt.lg_seq_id', $groping_id);
        $this->db->where('ltv.language_seq_id', $language_id);
        $this->db->group_by('lt.lt_data_group');
        $this->db->order_by('lt.lt_data_group', 'asc');
        return $this->result();
    }

    public function get_groupsOfGroupingLanguage($groping_id, $language_id, $page_group)
    {
        $this->db->from('metadata m');
        $this->db->join('metadata_values mv', 'mv.metadata_seq_id = m.metadata_seq_id', 'left');
        $this->db->where('m.lg_seq_id', $groping_id);
        $this->db->where('mv.language_seq_id', $language_id);
        $this->db->where('m.meta_data_group', $page_group);
        $result = $this->result();
        if ($result) {
            $return = array();
            foreach ($result as $r) {
                $return[$r->meta_data_key] = $r;
            }
            return $return;
        }
        return false;
    }

    public function get_groupsOfGroupingLanguage_text($groping_id, $language_id, $page_group)
    {
        $this->db->from('language_text lt');
        $this->db->join('language_text_value ltv', 'ltv.lt_seq_id = lt.lt_seq_id', 'left');
        $this->db->where('lt.lg_seq_id', $groping_id);
        $this->db->where('ltv.language_seq_id', $language_id);
        $this->db->where('lt.lt_data_group', $page_group);
        $result = $this->result();
        if ($result) {
            $return = array();
            foreach ($result as $r) {
                $return[$r->lt_data_key] = $r;
            }
            return $return;
        }
        return false;
    }

    // Check from here
    public function get_group_data($group_name)
    {
        $this->db->from('metadata');
        $this->db->where('meta_data_group', $group_name);
        return $this->filter_group_data($this->result());
    }

    private function filter_group_data($data)
    {
        $return = array();
        if (!$data)
            return false;
        foreach ($data as $d) {
            $return[$d->meta_data_key] = $d;
        }
        return $return;
    }

    public function get_group_names()
    {
        $this->db->from('metadata');
        $this->db->group_by('meta_data_group');
        $this->db->order_by('meta_data_group', 'asc');
        return $this->result();
    }


    public function update_BatchMetadata($data, $key)
    {
        $this->db->update_batch('metadata_values', $data, $key);
        return $this->db->affected_rows();
    }

    public function update_Batchlanguage_textdata($data, $key)
    {
        $this->db->update_batch('language_text_value', $data, $key);
        return $this->db->affected_rows();
    }
}
